﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

namespace TranscoderSharp
{
    public class LameConfig : ConfigObject
    {
        public static LameConfig Load()
        {
            if (File.Exists(XmlPath))
                return ObjectXMLSerializer<LameConfig>.Load(XmlPath);

            return new LameConfig();
        }

        public LameConfig()
        {

        }

        ~LameConfig()
        {
            this.Save();
        }

        public void Save()
        {
            string XmlFolder = Path.GetDirectoryName(XmlPath);
            if (!Directory.Exists(XmlFolder))
                Directory.CreateDirectory(XmlFolder);
            
            ObjectXMLSerializer<LameConfig>.Save(this, XmlPath);
        }

        public static string XmlPath
        {
            get
            {
                string m_AppDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Enceladus");
                return Path.Combine(m_AppDataPath, "LameConfig.xml");
            }
            set
            {
                string temp = value;
            }
        }

        public enum Target { Bitrate, Quality };
        public enum EncodingEngineQuality { Fast, Standard, High };
        public enum BitrateType { Average, Constant };

        private BitrateType m_BitrateEncodingType;

        public BitrateType BitrateEncodingType
        {
            get { return m_BitrateEncodingType; }
            set { m_BitrateEncodingType = value; }
        }

        private EncodingEngineQuality m_EngineQuality;// = EncodingEngineQuality.Standard;
        public EncodingEngineQuality EngineQuality
        {
            get { return m_EngineQuality; }
            set { m_EngineQuality = value; }
        }


        private Target m_EncodingTarget;// = Target.Quality;
        public Target EncodingTarget
        {
            get { return m_EncodingTarget; }
            set { m_EncodingTarget = value; }
        }


        private int m_VariableBitrateMode;// = 5; // can be 0..9 inclusive, 0 highest quality but slow, 9 low quality but fast
        public int VariableBitrateMode
        {
            get { return m_VariableBitrateMode; }
            set { m_VariableBitrateMode = value; }
        }

        private bool m_MonoEncoding;// = false;
        public bool MonoEncoding
        {
            get { return m_MonoEncoding; }
            set { m_MonoEncoding = value; }
        }

        private int m_Bitrate;// = 128;
        public int Bitrate
        {
            get { return m_Bitrate; }
            set { m_Bitrate = value; }
        }

        private bool m_Original;
        public bool Original
        {
            get { return m_Original; }
            set { m_Original = value; }
        }

        private bool m_Copyright;
        public bool Copyright
        {
            get { return m_Copyright; }
            set { m_Copyright = value; }
        }

        public string GetConfigString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("--add-id3v2 "); // this should ensure that both a v1 and v2 tag gets written
            //sb.Append("--proirity 3 ");

            if (m_MonoEncoding)
                sb.Append("-a ");

            if (m_Copyright)
                sb.Append("-c ");
            if (m_Original)
                sb.Append("-o ");

            // the following are shortcuts for the -q argument
            // fast = q7, standard = q5 and high = q2
            // defaults to q5
            if (m_EngineQuality == EncodingEngineQuality.Fast)
                sb.Append("-f ");
            if (m_EngineQuality == EncodingEngineQuality.High)
                sb.Append("-h ");
            

            if (m_EncodingTarget == Target.Quality)
            {
                sb.AppendFormat("-V {0} ", this.m_VariableBitrateMode);
            }
            else if (m_EncodingTarget == Target.Bitrate)
            {
                if (m_BitrateEncodingType == BitrateType.Constant)
                    sb.AppendFormat("--cbr -b {0} ", this.m_Bitrate);
                else if (m_BitrateEncodingType == BitrateType.Average)
                    sb.AppendFormat("--abr {0} ", this.m_Bitrate);
            }            

            return sb.ToString();
        }

        public bool ApplyMp3Gain { get; set; }
    }
}
